package jy.game.components.wuxue.pojo;


import java.util.ArrayList;
import java.util.List;

import jy.game.BasePojo;
import jy.game.components.wuxue.JyBuff;
import jy.game.components.wuxue.JyTrigger;
import jy.game.components.wuxue.SkillCoverType;
import jy.game.components.wuxue.SkillCoverTypeHelper;
import jy.game.tools.CommonSettings;
import jy.game.tools.JyResourceManager;
import wx.xml.simpleframework.xml.Attribute;
import wx.xml.simpleframework.xml.ElementList;
import wx.xml.simpleframework.xml.Root;

/**
 * 外功
 */
@Root(name = "skill")
public class JySkill extends BasePojo {
    @Attribute(name = "name") public                     String              name;
    @Attribute(name = "tiaohe") public                   int                 tiaoheValue  = -1;
    @Attribute(name = "type") public                     int                 type;
    @Attribute(name = "coversize") public                int                 coverSize;
    @Attribute(name = "castsize") public                 int                 castSize;
    @Attribute(name = "suit") public                     double              suit;
    @Attribute(name = "hard") public                     double              hard;
    @Attribute(name = "info") public                     String              info;
    @Attribute(name = "audio") public                    String              audio;
    @Attribute(name = "basepower") public                double              basePower;
    @Attribute(name = "step") public                     double              step;
    @Attribute(name = "animation") public                String              animation;
    @Attribute(name = "cd") public                       int                 coolDown;
    @Attribute public                                    String              icon         = "";
    @Attribute(name = "buff") public                     String              buff;
    @ElementList(inline = true, name = "level") public   List<JySkillLevel>  levels       = new ArrayList<>();
    @ElementList(inline = true, name = "trigger") public List<JyTrigger>     triggers     = new ArrayList<>();
    @ElementList(inline = true, name = "unique") public  List<JyUniqueSkill> uniqueSkills = new ArrayList<>();
    @Attribute(name = "covertype") public                int                 coverType;
    // private int _coverType = -1;

    @Override
    public /*override*/ String getPK() {
        return this.name;
    }

    public boolean getTiaohe() {
        return this.tiaoheValue == 1;
    }

    public int getCoverType() {
        return (this.coverType != -1) ? this.coverType : CommonSettings.GetDefaultCoverType(this.type).value();
    }

    public void setCoverType(int value) {
        this.coverType = value;
    }

    // [XmlIgnore]
    public Iterable<JyBuff> getBuffs() {
        {
            return JyBuff.Parse(this.buff);
        }
    }

    private JySkillLevel getSkillLevelInfo(int level) {
        for (JySkillLevel skillLevelInfo : this.levels) {
            if (skillLevelInfo.level == level) {
                return skillLevelInfo;
            }
        }
        return null;
    }

    public double getPower(int level) {
        JySkillLevel skillLevelInfo = this.getSkillLevelInfo(level);
        if (skillLevelInfo != null) {
            return skillLevelInfo.power;
        }
        return this.basePower + (level - 1) * this.step;
    }

    public int getCoverSize(int level) {
        JySkillLevel skillLevelInfo = this.getSkillLevelInfo(level);
        if (skillLevelInfo != null && skillLevelInfo.coverSize > 0) {
            return skillLevelInfo.coverSize;
        }
        if (this.coverSize > 0) {
            return this.coverSize;
        }
        if (this.getCoverType() == 5 && this.type == 2) {
            if (level < 5) {
                return 0;
            }
            if (level >= 5 && level < 100) {
                return 1;
            }
            if (level >= 100) {
                return 2;
            }
        }
        double dSize = new SkillCoverTypeHelper(SkillCoverType.build(this.coverType)).getdSize();
        if (level <= 10) {
            return (int) (1f + dSize * level);
        }
        return (int) (1f + dSize * 10f);
    }

    public SkillCoverType getCoverType(int level) {
        JySkillLevel skillLevelInfo = this.getSkillLevelInfo(level);
        if (skillLevelInfo != null) {
            return SkillCoverType.build(skillLevelInfo.coverType);
        }
        return SkillCoverType.build(this.getCoverType());
    }

    public int getCastSize(int level) {
        JySkillLevel skillLevelInfo = this.getSkillLevelInfo(level);
        if (skillLevelInfo != null && skillLevelInfo.castSize > 0) {
            return skillLevelInfo.castSize;
        }
        if (this.castSize > 0) {
            return this.castSize;
        }
        SkillCoverTypeHelper skillCoverTypeHelper = new SkillCoverTypeHelper(this.getCoverType(level));
        int                  baseCastSize         = skillCoverTypeHelper.getbaseCastSize();
        double               dCastSize            = skillCoverTypeHelper.getdCastSize();
        return (level > 10) ? ((int) (baseCastSize + dCastSize * 10f)) : ((int) (baseCastSize + dCastSize * level));
    }

    public String getAnimationName(int level) {
        JySkillLevel skillLevelInfo = this.getSkillLevelInfo(level);
        if (skillLevelInfo != null) {
            return skillLevelInfo.animation;
        }
        return this.animation;
    }

    public int getCoolDown(int level) {
        JySkillLevel skillLevelInfo = this.getSkillLevelInfo(level);
        if (skillLevelInfo != null) {
            return skillLevelInfo.coolDown;
        }
        return this.coolDown;
    }

    public int getLevelupExp(int currentLevel) {
        return (int) (currentLevel / 4f * (this.hard + 1f) / 4f * 15f * 8f);
    }

    public String getSuitInfo() {

        if (this.getTiaohe()) {
            return "阴阳调和";
        }
        if (this.suit == 0f) {
            return "无适性";
        }
        if (this.suit > 0f) {
            return String.format("阳%s%%", this.suit * 100f);
        }
        if (this.suit < 0f) {
            return String.format("阴%s%%", -this.suit * 100f);
        }
        return "错误适性";

    }

    @Override
    public /*override*/ void initBind() {
        for (JyUniqueSkill uniqueSkill : this.uniqueSkills) {
            JyResourceManager.add(JyUniqueSkill.class, uniqueSkill.getPK(), uniqueSkill);
        }
    }

    @Root(name = "level")
    public static class JySkillLevel {
        @Attribute(name = "level")
        public int    level;
        @Attribute(name = "covertype")
        public int    coverType;
        @Attribute(name = "coversize")
        public int    coverSize;
        @Attribute(name = "castsize")
        public int    castSize;
        @Attribute(name = "power")
        public double power;
        @Attribute(name = "animation")
        public String animation;
        @Attribute(name = "cd")
        public int    coolDown;
    }
}
